package com.snowleopard.common.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 分页的tag
 * 
 * @author LiuJian
 * 
 */
@StrutsTag(name = "paging", tldTagClass = "com.accentrix.vtc.hpp.common.tag.PageTag", description = "pageing")
public class Paging extends UIBean {

	private String ctx;
	private Page<?> page;

	@StrutsTagAttribute(description = "set page", type = "org.springside.modules.orm.Page")
	public void setPage(Page<?> page) {
		this.page = page;
	}

	@StrutsTagAttribute(description = "set ctx", type = "String")
	public void setCtx(String ctx) {
		this.ctx = ctx;
	}

	public Paging(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
		super(stack, request, response);
	}

	@Override
	protected String getDefaultTemplate() {
		//得到模板名,即 src/main/resources/template/simple/page.ftl
		return "page";
	}

	@Override
	protected void evaluateExtraParams() {
		super.evaluateExtraParams();

		if (null != ctx) {
			addParameter("ctx", ctx);
		}

		if (null != page) {
			addParameter("pre", page.getPrePage());
			addParameter("next", page.getNextPage());
			addParameter("total", page.getTotalPages());
			addParameter("no", page.getPageNo());
		}
	}

}
